<?php


namespace api\modules\v1\controllers\buss;

use api\controllers\OnAuthController;
use common\helpers\CommonHelper;
use common\models\buss\Scard;
use services\buss\ScardService;
use services\member\MemberService;
use Yii;

class ScardController extends OnAuthController
{
    public $modelClass = '';

    /**
     * @api {get} /v1/buss/scard/index 手机卡列表
     * @apiGroup Scard
     * @apiName index
     * @apiUse CommonParam
     *
     * @apiParam {String} keywords               搜索词
     * @apiParam {int} project_id                项目ID
     * @apiParam {int} authenticator_user        认证人
     * @apiParam {int} page
     * @apiParam {int} pageSize
     *
     * @apiSuccess {Object}  data
     * @apiSuccess {Array}   data.list                              列表数据
     * @apiSuccess {Number}  data.list.id                           列表ID
     * @apiSuccess {Number}  data.list.project_id                   项目ID
     * @apiSuccess {String}  data.list.project_name                 项目名
     * @apiSuccess {Number}  data.list.sim_card                     手机号
     * @apiSuccess {String}  data.list.sim_telecom                  运营商
     * @apiSuccess {String}  data.list.sim_telecom_name             运营商名称
     * @apiSuccess {Number}  data.list.belong_devices_id            设备ID
     * @apiSuccess {String}  data.list.devices_name                 设备名称
     * @apiSuccess {String}  data.list.use_mid                      使用人ID
     * @apiSuccess {String}  data.list.use_mname                    使用人
     * @apiSuccess {String}  data.list.authenticator_user           认证人
     * @apiSuccess {String}  data.list.recharge_remark              手机套餐
     * @apiSuccess {String}  data.list.remark                       备注
     * @apiSuccess {Number}  data.list.sort                         排序
     * @apiSuccess {Number}  data.list.status                       状态
     * @apiSuccess {String}  data.list.status_name                  状态名
     * @apiSuccess {String}  data.list.created_at
     * @apiSuccess {String}  data.list.updated_at
     * @apiSuccess {String}  data.condition
     * @apiSuccess {String}  data.condition.project_arr             项目筛选
     * @apiSuccess {String}  data.condition.authenticator_arr       认证人筛选
     * @apiSuccess {String}  data.condition.use_member_arr          使用人筛选
     * @apiSuccess {String}  data.condition.department_arr          部门筛选
     *
     */
    public function actionIndex()
    {
        $params = Yii::$app->request->get();
        $page = $params['page'] ?? 1;
        $pageSize = $params['pageSize'] ?? 10;
        $scardService = new ScardService();
        $list = $scardService->scardList($params, $page, $pageSize);

        // 筛选项
        $list['condition'] = [
            'project_arr' => MemberService::getProjectListCondition(),
            'authenticator_arr' => ScardService::getAuthenticatorCondition(),
            'use_member_arr' => ScardService::getUseMemberCondition(),
            'department_arr' => MemberService::getMemberDepartmentCondition()['list'],
        ];
        return $list;
    }

    /**
     * @api {get} /v1/buss/scard/edit-condition 编辑下拉筛选
     * @apiGroup Scard
     * @apiName edit-condition
     * @apiUse CommonParam
     *
     * @apiParam {int} id                   手机卡ID
     *
     * @apiSuccess {String}  data.condition
     * @apiSuccess {String}  data.condition.project_arr                 项目筛选
     * @apiSuccess {String}  data.condition.authenticator_arr           认证人筛选
     * @apiSuccess {String}  data.condition.sim_telecoms_arr            运营商筛选
     * @apiSuccess {String}  data.condition.department_arr              部门筛选
     *
     */
    public function actionEditCondition(): array
    {
        $params = Yii::$app->request->get();
        $id = $params['id'] ?? 0;
        // 筛选项
        $scard = Scard::findOne($id);
        $list['condition'] = [
            'project_arr' => MemberService::getProjectEditCondition($scard->project_id ?? null),
            'authenticator_arr' => ScardService::getAuthenticatorCondition(),
            'sim_telecoms_arr' => ScardService::getSimTelecomsCondition(),
            'department_arr' => MemberService::getMemberDepartmentCondition()['list'],
        ];
        return $list;
    }

    /**
     * @api {get} /v1/buss/scard/get-member-condition 人员下拉列表
     * @apiGroup Scard
     * @apiName get-member-condition
     * @apiUse CommonParam
     *
     * @apiParam {int} project_id                项目ID
     * @apiParam {int} department_id             部门ID
     *
     * @apiSuccess {Object}  data
     * @apiSuccess {Array}   data.value              人员ID
     * @apiSuccess {Number}  data.label              人员名称
     */
    public function actionGetMemberCondition(): array
    {
        $params = Yii::$app->request->get();
        $params['project_id'] = isset($params['project_id']) && $params['project_id'] ? $params['project_id'] : null;
        $params['department_id'] = isset($params['department_id']) && $params['department_id'] ? $params['department_id'] : null;
        return MemberService::getMemberByProject($params['project_id'], $params['department_id']);
    }

    /**
     * @api {post} /v1/buss/scard/update-scard 更新手机卡
     * @apiGroup Scard
     * @apiName update-scard
     * @apiUse CommonParam
     *
     * @apiBody {Number}   id                                      手机卡ID【编辑时必传】
     * @apiBody {String}   sim_card                                手机卡号
     * @apiBody {Number}   project_id                              项目ID
     * @apiBody {Number}   sim_telecom                             运营商【1：电信 2：联通 3：移动 4：光电】
     * @apiBody {String}   authenticator_user                      认证人
     * @apiBody {String}   remark                                  备注
     * @apiBody {Number}   status                                  状态【-1：删除、0：禁用、1：启用】
     *
     * @apiSuccess {Number} data    新增/编辑ID
     */
    public function actionUpdateScard()
    {
        $params = Yii::$app->request->post();
        if (!isset($params['sim_card']) || !$params['sim_card']) {
            return CommonHelper::api(422, '手机卡号必填');
        }
        $params['id'] = $params['id'] ?? 0;
        $scardService = new ScardService();
        return $scardService->updateScard($params);
    }

    /**
     * @api {post} /v1/buss/scard/delete-scard 删除手机卡
     * @apiGroup Scard
     * @apiName delete-scard
     * @apiUse CommonParam
     *
     * @apiBody {String} id     ID
     *
     * @apiSuccess {Object} data    返回数据
     */
    public function actionDeleteScard()
    {
        $params = Yii::$app->request->post();
        if (!isset($params['id']) || !$params['id']) {
            return CommonHelper::api(422, '缺少数据ID');
        }

        $scardService = new ScardService();
        return $scardService->deleteScard($params);
    }

    /**
     * @api {get} /v1/buss/scard/recharge-list 充值列表
     * @apiGroup Scard
     * @apiName recharge-list
     * @apiUse CommonParam
     *
     * @apiParam {String} id        手机卡ID
     * @apiParam {String} recharge_stime        充值开始时间
     * @apiParam {String} recharge_etime        充值结束时间
     * @apiParam {int} page
     * @apiParam {int} pageSize
     *
     * @apiSuccess {Object}  data
     * @apiSuccess {Array}   data.list                                          列表数据
     * @apiSuccess {Number}  data.list.scard_id                                 手机卡ID
     * @apiSuccess {Number}  data.list.recharge_money                           充值金额
     * @apiSuccess {Number}  data.list.recharge_time
     * @apiSuccess {Number}  data.list.recharge_time_str                        充值日期
     * @apiSuccess {String}  data.list.operation_id                             操作人ID
     * @apiSuccess {String}  data.list.operation_name                           操作人名
     * @apiSuccess {String}  data.recharge_remark                              手机套餐
     */
    public function actionRechargeList()
    {
        $params = Yii::$app->request->get();
        if (!isset($params['id']) || !$params['id']) {
            return CommonHelper::api(422, '缺少数据ID');
        }

        $page = $params['page'] ?? 1;
        $pageSize = $params['pageSize'] ?? 10;

        $scardService = new ScardService();
        $list = $scardService->rechargeList($params, $page, $pageSize);
        $scard = Scard::findone($params['id']);
        $list['recharge_remark'] = $scard->recharge_remark ?? '';
        return $list;
    }

    /**
     * @api {post} /v1/buss/scard/create-recharge 新增充值
     * @apiGroup Scard
     * @apiName create-recharge
     * @apiUse CommonParam
     *
     * @apiBody {Number}   scard_id                                     手机卡ID
     * @apiBody {String}   recharge_remark                              套餐备注
     * @apiBody {Number}   recharge_money                               充值金额
     * @apiBody {Number}   recharge_time                                时间
     *
     * @apiSuccess {Number} data    手机卡ID
     */
    public function actionCreateRecharge()
    {
        $params = Yii::$app->request->post();
        if (!isset($params['scard_id']) || !$params['scard_id']) {
            return CommonHelper::api(422, '手机卡号必传');
        }
        if (!isset($params['recharge_time']) || !$params['recharge_time']) {
            return CommonHelper::api(422, '充值时间必填');
        }
        if (!isset($params['recharge_money']) || !$params['recharge_money']) {
            return CommonHelper::api(422, '充值金额必填');
        }
        $params['id'] = $params['id'] ?? 0;
        $scardService = new ScardService();
        return $scardService->createRecharge($params);
    }

    /**
     * @throws \Exception
     * @api {post} /v1/buss/scard/delete-recharge 删除充值记录
     * @apiGroup Scard
     * @apiName delete-recharge
     * @apiUse CommonParam
     *
     * @apiBody {Number} recharge_id     充值ID
     *
     * @apiSuccess {Object} data    返回数据
     */
    public function actionDeleteRecharge()
    {
        $rechargeId = Yii::$app->request->post('recharge_id');
        if (!$rechargeId) {
            return CommonHelper::api(422, '缺少充值ID');
        }

        $service = new ScardService();
        return $service->deleteRecharge($rechargeId);
    }
}
